/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the ViewMyCurrency extension.
 *
 * The Initial Developer of the Original Code is
 * Will Moffat.
 * Portions created by the Initial Developer are Copyright (C) 2006
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

//dump("// $Id: WILL_CurrencyListBox.js,v 1.15 2007/02/26 11:01:54 Will Exp $;\n");

//HACK: CurrencyListBox needs to be updated to handle Conversion Profiles??

// WILL: ?? because of the inferface this implements I cannot use additional properities from outside the object
VMC.TreeView = function(aList,highlightDups) {
  var aserv=Components.classes["@mozilla.org/atom-service;1"].getService(Components.interfaces.nsIAtomService);
  this.list=aList;
  this.highlightDups=highlightDups;
  this.rowCount = this.list.items.length;
  //WILL: TODO: Support column object for Firefox 1.1
  this._symbolCol = function(colName) {
    var symbolscol="symbolscol";
    if (colName.substr(0,symbolscol.length) == symbolscol) {
      var digit=colName[symbolscol.length]; //return the final digit in symbolscol0, col1 etc
      return digit;
    }
    return -1;
  };
  this._col2symbol = function(colName,curr) {
    var digit=this._symbolCol(colName);
    if (digit<0) { return null; }
    //WILL: TODO: only supporting one symbol: if (digit>=curr.symbols.length) { return ' '; }     return curr.symbols[digit];
    return curr.displaySymbol;
  };
  this.getCellText = function(row,colObj){
    if (row<0 || row>=this.list.items.length) { return null; } //WILL: log?
    var colName=VMC.toColName(colObj);
    var digit,symbol;
    var curr=this.list.items[row];
    if      (colName == "countrycol")                   { return curr.country;              }
    else if (colName == "descriptioncol")               { return curr.description;          }
    else if (colName == "codecol")                      { return curr.code;                 }
    //WILL: TODO:    else if (colName == "dspIndexcol")                  { return curr.displaySymbolIndex;   }
    else if ( (symbol=this._col2symbol(colName,curr)) ) { return symbol;                    }
    else                                               { return "????";                    }
  };
  this.setTree = function(treebox){ this.treebox = treebox; };
  this.isEditable = function(index, colObj) { alert('h'); return false; };
  this.isContainer = function(row){ return false; };
  this.isSeparator = function(row){ return false; };
  this.isSorted = function(row){ return false; };
  this.getLevel = function(row){ return 0; };
  this.getImageSrc = function(row,col){ return null; };
  this.getRowProperties = function(row,props){};
  //this.getCellProperties = function(row,col,props) {};
  this.getCellProperties = function(row,colObj,props){
    return; //WILL: TODO: no highlighting supported at all
    var colName=VMC.toColName(colObj);
    var digit=this._symbolCol(colName);
    if (digit>-1) {
      //WILL: TODO: if (digit==this.list.items[row].displaySymbolIndex)  { props.AppendElement(aserv.getAtom("VMC-highlightDisplaySymbol"));  }
      
      //WILL: TODO: could hightlight dup codes, but dup symbols is an issue for the symbol table editor, not here
      if (this.highlightDups) {
	var str=this.getCellText(row,colObj);
	if (!str) { return; }
	if (!this.list.isUnique(str)) {
	  props.AppendElement(aserv.getAtom("VMC-highlight")); //this str is used by another curr - so highlight it
	}
      }
    }
  };

  this.getColumnProperties = function(colid,colObj,props){};
  this.getParentIndex = function(row){return -1;};
  this.cycleHeader = function(colObj, elem) { };
  this.performAction = function(action) {
    //alert('got action '+action);
    //assume invalidate - I'm too lazy to calculate which row changed
    this.treebox.beginUpdateBatch();
    this.rowCount = this.list.items.length;
    this.treebox.endUpdateBatch();
  };
};


VMC.toColName = function(colObj) {
  if (!colObj) { return undefined; } // WILL: who does this?
  var colName;
  if (typeof colObj == "string") {
    colName=colObj;
  } else { // it's a treeColumn object
    colName=colObj.id;
  }
  if (!colName) { VMC.log('ERR','invalid column'+colObj); }
  return colName;
};
